﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;

namespace MShop.Core.DataAccess
{
    public class TradeDao:CommonDao<Trade>,ITradeDao
    {
        public TradeDao(IObjectContextProvider provider)
            : base(provider)
        {
        }

        public override void Delete(object obj)
        {
            if (obj is IEnumerable<Trade>)
            {
                foreach (var item in (IEnumerable<Trade>)obj)
                {
                    this.Delete(item);
                }
            }
            else
            {
                base.Delete(obj);
            }
        }

        public override void Delete(Trade entity)
        {
            base.Delete(entity.Order);
            base.Delete(entity);
        }

        
        /// <summary>
        /// 删除旧的订单
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="span"></param>
        public void DeleteInvalidTrade(Guid userid, TimeSpan span)
        {
             //删除旧的订单
            DateTime dt = DateTime.Now.AddMinutes(-span.TotalMinutes);
            var deletequery = Find(x => x.UserId == userid && x.TradeStatus == (int)TradeStatus.无效订单 && x.Created <=dt );
            Delete(deletequery);         
            //删除旧的订单
        }

        
    }
}